<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\Collect;
use Illuminate\Http\Request;

class CollectController extends BaseController
{
    /**
     * 收藏、取消收藏
     */
    public function index(Request $request)
    {
        $products_id=$request->input('products_id');
        if(empty($products_id)) return $this->apiError('products_id不能為空');
        $users_id=$request->userId;
      $model= Collect::where('users_id',$users_id)->where('products_id',$products_id)->first();
      if(!$model){
        $model=new Collect();
        $model->users_id=$users_id;
        $model->products_id=$products_id;
        $model->save();
      }else{
        $model->delete();
      }
      return $this->success();
    }
    /**
     * 我的收藏
     */
    public function getCollect(Request $request)
    {
       $data=Collect::where('users_id',request()->userId)->with('products')->paginate($request->input('limit',10));

       return $this->success($data);
    }

}
